
var cityList = [
    { key: 'A', data: [{"id":"100000","name":"安丘市"},{"id":"100001","name":"安乡县"}]},
    { key: 'B', data: [{"id":"100002","name":"亳州市"},{"id":"100003","name":"保山市"}]},
    { key: 'C', data: [{"id":"100004","name":"常宁市"},{"id":"100005","name":"常州市"}]},
    { key: 'D', data: [{"id":"100006","name":"丹阳县"},{"id":"100007","name":"当涂县"}]},
    { key: 'E', data: [{"id":"100008","name":"峨眉山市"},{"id":"100009","name":"额尔古纳市"}]},
    { key: 'G', data: [{"id":"100010","name":"广州市"},{"id":"100011","name":"光泽县"}]},
    { key: 'H', data: [{"id":"100012","name":"黄山"},{"id":"100013","name":"华山"}]},
    { key: 'J', data: [{"id":"100014","name":"九华山"},{"id":"100015","name":"九台市"}]},
    { key: 'K', data: [{"id":"100016","name":"克拉玛依市"}]},
  ] 
var rightLetter = ["A","B","C","D","E","G","H","J","K"];  
var hotCity = [{"id":"100000","name":"安丘市"},{"id":"100001","name":"安乡县"}]

//选中的城市id
var selectId="";
//选中的城市名称
var selectName="";
//回调函数
var clickCallBackFn="";
var cacheName = "";
/**
 * 选择器初始化方法
 *
 * @param objId 对象id
 * @param hotList 热门对象集合，格式如hotCity
 * @param contentList 所有对象集合，格式如cityList
 * @param rightLetterList 右边导航集合，格式如rightLetter
 * @param tip 热门标识，默认是热门城市
 * @param clickFn 点击，回调函数
 * @author shengQiang yu
 */
function initSelectData(objId,cacheNames,hotList,contentList,rightLetterList,tip,clickFn) {
	  clickCallBackFn=clickFn;
	  //设置热门内容
	  if(tip==""||tip==null){
	      tip="热门城市";
	  }
	  if (cacheNames == null || cacheNames == "") {
          cacheName == "hotelHistory";
      } else {
          cacheName = cacheNames;
      }
      $('#'+objId+' .city').html('');
      var hotHtml = '';
      hotHtml += '<div class="tips" id="热门1">'+tip+'</div>';
      hotHtml += '<div class="hot hotCity">';
      $.each(hotList, function (i, item) {
          hotHtml += '<div data-id="' + item.id + '">' + item.name + '</div>'
      })
      hotHtml += '</div>';
      hotHtml += '<div class="history"></div>';
      $('#'+objId+' .city').append(hotHtml);
	  //设置右边导航
	   var rightLetterHtml = '';
      $.each(rightLetterList, function (i, item) {
          rightLetterHtml += '<li><a href="javascript:;">' + item + '</a></li>';
      })
      $('#'+objId+' .letter ul').append(rightLetterHtml);
	  
	  //设置历史
      initHistory();
      //设置内容
      var html = '';
      $.each(contentList, function (i, item) {
          html += '<div class="city-list"><span class="city-letter" id="' + item.key + '1">' + item.key + '</span>';
          $.each(item.data, function (j, data) {
              html += '<p  data-id="' + data.id + '">' + data.name + '</p>';
          })
          html += '</div>';
      })
      $('#'+objId+' .city').append(html);
	  
	  
      // 选择城市
      $('body').on('click', '.city-list p', function () {
		   selectName = $(this).text();
		   selectId = $(this).attr("data-id");
           saveHistory(selectId+"&"+selectName);
      });
  
      $('.hot.hotCity').on('click', 'div', function () {
          selectName = $(this).text();
		  selectId = $(this).attr("data-id");
          saveHistory(selectId+"&"+selectName);
      });
  }
  
  
  function bindHistoryClick() {
    $('.history .hot').on('click', 'div', function () {
        selectName = $(this).text();
		selectId = $(this).attr("data-id");
        saveHistory(selectId+"&"+selectName);
    });
    
  }
  
  function clearHistory() {
    localStorage.setItem(cacheName, '');
    initHistory();
  }
  function saveHistory(data) {
    var hotelHistory = localStorage.getItem(cacheName);
    if(!hotelHistory){
      hotelHistory = '';
    }
    if(hotelHistory.indexOf(data) > -1) {
      var arr = hotelHistory.split(',');
      var newArr = [];
      $.each(arr, function (i, item) {
        if(item !== data) {
          newArr.push(item);
        }
      })
      hotelHistory = newArr.join(',');
    }
    localStorage.setItem(cacheName, data + ',' + hotelHistory);
    initHistory();
  }
  
  
  
  function initHistory() {  // 历史记录渲染
    var hotelHistory = localStorage.getItem(cacheName);
      var history = '';
      history += '<div class="tips" id="历史1">历史记录<div class="clear-history" onClick="clearHistory();">清空历史</div></div>';
      history += '<div class="hot">';
      if(hotelHistory) {
        $.each(hotelHistory.split(','), function (i, item) {
          if(item) {
			  var a=item.split("&");
            history += "<div data-id='"+ a[0] + "' class='blue'>"+ a[1] + "</div>"
          }
        })
      }
      history += '</div>';
      $('.history').html(history);
      bindHistoryClick();
	  if (selectId!=null&&selectName!=null&&selectId!=""&&selectName!=""&&(typeof clickCallBackFn == 'function')) {
        clickCallBackFn(selectId,selectName);
    }
  }; (function ($) {
      $('.letter').bind("touchstart touchmove", function (e) {
          var top = $(window).scrollTop();
          e.preventDefault();//阻止默认滚动
          var touch = e.touches[0];
          var ele = document.elementFromPoint(touch.pageX, touch.pageY - top);
          if (ele.tagName === 'A') {
              var s = $(ele).text();
              $(window).scrollTop($('#' + s + '1').offset().top)
              $("#showLetter span").html(s.substring(0, 1));
              $("#showLetter").show().delay(500).hide(0);
          }
      });
  
      $('.letter').bind("touchend", function (e) {
          $("#showLetter").hide(0);
      });
  
  })(Zepto); 
  